﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class LienHe : Form
    {
        public LienHe()
        {
            InitializeComponent();
        }

        private void LoadDataGridViewLienHe()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from LienHe";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "LienHe");
            DataTable dt = ds.Tables["LienHe"];

            dataGridViewLienHe.DataSource = ds.Tables["LienHe"].DefaultView;
        }

        private void LienHe_Load(object sender, EventArgs e)
        {
            LoadDataGridViewLienHe();
        }

        private void buttonCapNhatLienHe_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Bạn có muốn sửa dự liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
            {
                int i = dataGridViewLienHe.SelectedRows[0].Index;
                int id = int.Parse(dataGridViewLienHe.Rows[i].Cells["MaSo_TLH"].Value.ToString());

                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "update LienHe set TenCongTy_NLH = @ten, HoTen_NLH = @hotennlh, DiaChi_NLH = @dc, SDT_NLH = @dt, Email_NLH = @email, NoiDung_NLH = @nd where MaSo_TLH = @idnlh";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@idnlh", id);
                cmd.Parameters.AddWithValue("@ten", textBoxTenCongTy_NLH.Text);
                cmd.Parameters.AddWithValue("@hotennlh", textBoxHoTen_NLH.Text);
                cmd.Parameters.AddWithValue("@dc", textBoxDiaChi_NLH.Text);
                cmd.Parameters.AddWithValue("@dt", textBoxSDT_NLH.Text);
                cmd.Parameters.AddWithValue("@email", textBoxEmail_NLH.Text);
                cmd.Parameters.AddWithValue("@nd", textBoxNoiDung_NLH.Text);
                

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                LoadDataGridViewLienHe();
            }
        }

        private void buttonXoaLienHe_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Bạn có muốn xóa dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) ==
                DialogResult.OK)
            {
                int i = dataGridViewLienHe.SelectedRows[0].Index;
                int id = int.Parse(dataGridViewLienHe.Rows[i].Cells["MaSo_TLH"].Value.ToString());

                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "delete from LienHe where MaSo_TLH = @idtlh";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@idtlh", id);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                dataGridViewLienHe.Rows.RemoveAt(i);

                textBoxTenCongTy_NLH.Text = "";
                textBoxHoTen_NLH.Text = "";
                textBoxDiaChi_NLH.Text = "";
                textBoxSDT_NLH.Text = "";
                textBoxEmail_NLH.Text = "";
                textBoxNoiDung_NLH.Text = "";
            }
        }

        private void dataGridViewLienHe_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int id = int.Parse(dataGridViewLienHe.Rows[e.RowIndex].Cells["MaSo_TLH"].Value.ToString());

            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from LienHe where MaSo_TLH = @idtlh";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@idtlh", id);

            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                textBoxTenCongTy_NLH.Text = reader["TenCongTy_NLH"].ToString();
                textBoxHoTen_NLH.Text = reader["HoTen_NLH"].ToString();
                textBoxDiaChi_NLH.Text = reader["DiaChi_NLH"].ToString();
                textBoxSDT_NLH.Text = reader["SDT_NLH"].ToString();
                textBoxEmail_NLH.Text = reader["Email_NLH"].ToString();
                textBoxNoiDung_NLH.Text = reader["NoiDung_NLH"].ToString();
            }
            reader.Close();
            conn.Close();
        }
    }
}
